# -*- coding: utf-8 -*-
import os

import win32api
import win32com
import win32print
import argparse

from win32com.client import Dispatch

sumatraPDFPath = "D:\handeprint\SumatraPDF-3.3.3-32.exe"


def printer_loading(filename):
    open(filename, "r")
    win32api.ShellExecute(0, "print", filename,
                          '/d:"%s"' % win32print.GetDefaultPrinter(), ".", 2)


def print_word(word_file_path):

    exec_tool = 'kwps.application'
    word = win32com.client.Dispatch(exec_tool)
    word.Visible = 0
    word.DisplayAlerts = 0
    doc = word.Documents.Open(word_file_path)
    doc.PrintOut()

    doc.Close()
    word.Quit()


def print_excel(excel_file_path):
    try:
        #office excel.application
        exec_tool = 'EXCEL.Application'
        application = win32com.client.Dispatch(exec_tool)
        application.Visible = 0
        application.DisplayAlerts = 0
        print(application)
        excel = application.Workbooks.Open(excel_file_path)
        excel.PrintOut()

        excel.Close()
        application.Quit()
    except Exception as e:
        print("error:", e.message)


def print_pdf(pdf_file_path):
    pdf_print_cmd = "%s -print-to-default  %s" % (sumatraPDFPath,
                                                  pdf_file_path)
    pdfLog = os.popen(pdf_print_cmd)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Test for argparse')
    parser.add_argument('--filepath',
                        '-f',
                        help='file full path of  print file',
                        required=True)
    parser.add_argument('--type', '-t', help='defalut word', required=True)
    args = parser.parse_args()
    # print args
    try:
        if "document" in args.type:
            print_word(args.filepath)
        elif "sheet" in args.type:
            print_excel(args.filepath)
        elif "pdf" in args.type:
            print_pdf(args.filepath)
        else:
            print("is not supported")
    except Exception as e:
        print "error_message:"